package com.kystar.muxplayer.ui;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Size;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.kystar.muxplayer.BaseApp;
import com.kystar.muxplayer.Helper;
import com.kystar.muxplayer.R;
import com.kystar.muxplayer.base.Const;
import com.kystar.muxplayer.model.Settings;
import com.kystar.muxplayer.protocol.code.set.CS03_ConcatMode;
import com.kystar.muxplayer.protocol.code.set.CS04_OutPut;
import com.kystar.muxplayer.protocol.code.set.CS05_ScreenSize;
import com.kystar.muxplayer.selfview.MyEditText;
import com.kystar.muxplayer.selfview.MyKeyBoard;

/**
 * Created by zhusheng on 2018-03-10. <p>
 */
public class OutputGroupView extends GroupView {

    private View customSizeView;
    private OkCancelView okCancelSize;
    private OkCancelView okCancelHz;
    private OkCancelView okCancelDetail;
    MyKeyBoard mKeyBoard;

    public OutputGroupView(Context context, MyKeyBoard myKeyBoard) {
        super(context, "拼接设置");
        mKeyBoard = myKeyBoard;
        addSubGroup("拼接模式", R.layout.layout_main_settings_concat_mode, false);
        addSubGroup("输出分辨率", R.layout.layout_main_settings_concat_size, false);
        addSubGroup("屏幕参数", R.layout.layout_main_settings_concat_screen, true);

        customSizeView = mContentView.findViewById(R.id.concat_size_custom);

        setupConcatMode();
        setupConcatSize();
        setupCustomView();
        setupOutputSize();

        updateView();

    }

    private void setupConcatMode() {
        ViewList group = new ViewList(mContentView, R.id.concat_mode, false);

        group.select(BaseApp.getSettings().getOutput().getConcatIndex() - 1, false);
        group.setOnClickListener(v -> {
            int tag = Helper.getTagInt(v, 0);
            // 拼接模式点击事件
            Toast.show(tag+"");
            BaseApp.send(CS03_ConcatMode.create(tag)).subscribe(cmd -> {
            }, e -> {
                e.printStackTrace();
            }, BaseApp::updateOutPut);
        });
    }

    private void setupConcatSize() {
        final GridView gridView = mContentView.findViewById(R.id.concat_size_list);
        final ViewList viewList = new ViewList(mContentView, R.id.concat_size_buttons, true);
        final TextView sizeIndicator = mContentView.findViewById(R.id.concat_size_indicator);

        viewList.setOnClickListener(v -> {
            if ("0".equals(v.getTag().toString())) {
                Helper.hideView(customSizeView);
                Helper.showView(gridView);
            } else {
                Helper.showView(customSizeView);
                Helper.hideView(gridView);
            }
            sizeIndicator.setX(v.getLeft() + v.getWidth() / 2 - sizeIndicator.getWidth() / 2);
        });

        final Holder<View> lastSizeView = new Holder<>();
        gridView.setAdapter(new ArrayAdapter<String>(
            getContext(), R.layout.textview_simple, Const.OUTPUT_SCREEN_SIZE) {
            @Override
            public View getView(int position, @Nullable View convertView,
                @NonNull ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                if (position == BaseApp.getSettings().getOutput().getScreenSizeId()) {
                    lastSizeView.setValue(view);
                    Helper.setChildTextColor(view, 0, true);
                }
                return view;
            }
        });
        gridView.setOnItemClickListener(((parent, view, position, id) -> {
            Helper.setChildTextColor(lastSizeView.getValue(), 0, false);
            Helper.setChildTextColor(view, 0, true);
            lastSizeView.setValue(view);
            BaseApp.send(CS04_OutPut.resolution(position + 1)).subscribe(cmd -> {

            }, e -> {

                int select = BaseApp.getSettings().getOutput().getScreenSizeId();
                if (select >= 1 && select <= 8) {
                    viewList.select(select, false);
                }
            }, this::reloadOutPut_Pvw);
        }));
        int select = BaseApp.getSettings().getOutput().getScreenSizeId();
        if (select >= 1 && select <= 8) {
            viewList.select(select, false);
        }
    }

    private void setupCustomView() {
        final MyEditText sizeH = customSizeView.findViewById(R.id.concat_size_h);
        final MyEditText sizeV = customSizeView.findViewById(R.id.concat_size_v);
        sizeH.setOnClickListener(v ->{
            mKeyBoard.show((MyEditText) v);
        });
        sizeV.setOnClickListener(v ->{
            mKeyBoard.show((MyEditText) v);
        });
        final ViewList viewListHz = new ViewList(customSizeView, R.id.concat_size_hz_list, false);

        okCancelSize = new OkCancelView(
            customSizeView.findViewById(R.id.output_custom_size_buttons));
        okCancelSize.watch(true, sizeH, sizeV).onOk(() -> {
            Size size = Helper.parseSize(sizeH, sizeV);
        }).onCancel(() -> {
//            sizeH.setText(String.valueOf(settings.output.screenWidth));
//            sizeV.setText(String.valueOf(settings.output.screenHeight));
        }).hide();

        okCancelHz = new OkCancelView(customSizeView.findViewById(R.id.output_custom_hz_buttons));
        okCancelHz.onOk(() -> {
            int tag = Helper.getTagInt(viewListHz.selected(), 0);
//            settings.output.frequency = tag;
//            connection.run(p -> p.setOutputFreq(tag)).post();
        }).onCancel(() -> {
            // viewListHz.select(String.valueOf(settings.output.frequency), true);
        }).hide();

        viewListHz.setOnClickListener(v -> okCancelHz.show());
    }

    private void setupOutputSize() {
        final ViewList outputList = new ViewList(mContentView, R.id.concat_size_out_list, true);
        final View rectView = mContentView.findViewById(R.id.concat_size_out_content);
        final TextView indicator = ((View) rectView.getParent())
            .findViewById(R.id.concat_out_indicator);
        final MyEditText sizeH = rectView.findViewById(R.id.concat_size_h);
        final MyEditText sizeV = rectView.findViewById(R.id.concat_size_v);
        sizeH.setOnClickListener(v -> {
            mKeyBoard.show((MyEditText) v);
        });
        sizeV.setOnClickListener(v -> {
            mKeyBoard.show((MyEditText) v);
        });

        okCancelDetail = new OkCancelView(
            mContentView.findViewById(R.id.concat_size_detail_buttons));
        okCancelDetail.watch(true, sizeH, sizeV).onOk(() -> {
            try {
                int index = Helper.getTagInt(outputList.selected(), 0);
                Size size = Helper.parseSize(sizeH, sizeV);
                Settings settings = BaseApp.getSettings();
                settings.getOutput().getSizes()[index] = size;
                BaseApp.send(CS05_ScreenSize.create(settings.getOutput().getSizes()))
                    .subscribe(cmd -> {
                    }, e -> {
                    }, this::reloadOutPut_Pvw);
//                settings.output.setSize(index, size.getWidth(), size.getHeight());
//                muxModel.editOutput(p->p.setOutputSizeDetail(settings.output.sizes));
            } catch (Exception e) {
                e.printStackTrace();
                Helper.show(e.getMessage());
            }
        }).onCancel(() -> {
            int index = Helper.getTagInt(outputList.selected(), 0);
            Size size = BaseApp.getSettings().getOutput().getSizes()[index];
            sizeH.setText(String.valueOf(size.getWidth()));
            sizeV.setText(String.valueOf(size.getHeight()));
            okCancelDetail.hide();
        }).hide();
        outputList.setOnClickListener(view -> {
            okCancelDetail.cancel();
            indicator.setX(view.getLeft() + view.getWidth() / 2 - indicator.getWidth() / 2);
        });
        outputList.select(0, true);
    }

    void updateView() {
        okCancelSize.cancel();
        okCancelHz.cancel();
        okCancelDetail.cancel();
    }

    private void reloadOutPut_Pvw() {

    }
}
